﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wjtour.Common;
using Hjp.FrameWork.Common;
using Wjtour.BLL;
using System.Linq;
using TongCheng.API.OpenAPI;
using TongCheng.API.OpenAPI.Domain;
using TongCheng.API.OpenAPI.Request;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using TongCheng.API.OpenAPI.Facade;

namespace Wjtour.Web.Front
{
    public partial class hotellist : BasePage
    {
        #region 固定分页参数
        protected int page = 1;
        protected int pagesize = 3;
        #endregion

        slides_bll _slides_bll = new slides_bll();
        protected PaginationData<Wjtour.Domain.scene> data = new PaginationData<Domain.scene>();
        protected int type = 1;
        protected List<Wjtour.Domain.line> recommendLines = new List<Domain.line>();
        Wjtour.BLL.line_bll _linebll = new line_bll();
        protected List<Wjtour.Domain.article> trafficGuides = new List<Domain.article>();//旅游攻略
        Wjtour.BLL.article_bll _articleBll = new article_bll();
        protected List<Wjtour.Domain.slides> _slides = new List<Domain.slides>();

        protected TongCheng.API.OpenAPI.Domain.HotelList list = new TongCheng.API.OpenAPI.Domain.HotelList();
        protected Dictionary<int, HotelRoomList> hotelRoomsDic = new Dictionary<int, HotelRoomList>();

        protected TextBox txtComeDate;
        protected TextBox txtLeaveDate;
        protected TextBox txtLocation;
        protected TextBox txtHotelName;
        protected TextBox txtSceneryName;
        protected Button hotelSearch, scenerySearch;

        #region 参数
        protected string comeDate = DateTime.Now.ToString("yyyy-MM-dd");
        protected string leaveDate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
        string cd;
        string ld;
        string loc;
        string n;
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            page = Utils.StrToInt(Request["page"], 1);
            hotelSearch.Click += hotelSearch_Click;
            scenerySearch.Click += scenerySearch_Click;

            bindList();
            cd = Request["cd"];
            ld = Request["ld"];
            loc = Request["loc"];
            n = Request["n"];

            if (!string.IsNullOrEmpty(cd))
            {
                comeDate = cd;
                txtComeDate.Text = comeDate;
            }
            if (!string.IsNullOrEmpty(ld))
            {
                leaveDate = ld;
                txtLeaveDate.Text = leaveDate;
            }
            if (!string.IsNullOrEmpty(loc))
            {
                txtLocation.Text = loc;
            } 
            if (!string.IsNullOrEmpty(n))
            {
                txtHotelName.Text = n;
            }
            if (!IsPostBack)
            {
                getHotels();
            }
        }

        void scenerySearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtSceneryName.Text))
            {
                string query = "s=" + HttpUtility.UrlEncode(txtSceneryName.Text);
                Response.Redirect("ticketlist.aspx?" + query);
            }
            Response.Redirect("ticketlist.aspx");
        }

        void hotelSearch_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtComeDate.Text))
            {
                Utils.Alert(this.Page, "请选择入住日期！");
                return;
            }
            if (string.IsNullOrEmpty(txtLeaveDate.Text))
            {
                Utils.Alert(this.Page, "请选择离店日期！");
                return;
            }
            DateTime dt1 = DateTime.Now;
            DateTime dt2 = DateTime.Now.AddDays(1);
            DateTime.TryParse(txtComeDate.Text, out dt1);
            DateTime.TryParse(txtLeaveDate.Text, out dt1);
            if (dt1 > dt2)
            {
                Utils.Alert(this.Page, "入住日期不能晚于离店日期！");
                return;
            }
            comeDate = txtComeDate.Text;
            leaveDate = txtLeaveDate.Text;
            getHotels();
        }

        void bindList()
        {
            _slides = _slides_bll.GetList(5);
            recommendLines = _linebll.GetAll<Wjtour.Domain.line>("select top 6 * from line where isrecommend=1").ToList();
            trafficGuides = _articleBll.GetAuditTopListWithFirstLogo("6", 7, 2);
        }

        void getHotels()
        {
            GetHotelListRequest request = new GetHotelListRequest();
            request.Body.clientIp = "172.0.0.1";
            request.Body.cityId = 226;
            request.Body.sectionId = 6325;
            request.Body.comeDate = comeDate;
            request.Body.leaveDate = leaveDate;
            request.Body.page = page;
            request.Body.pageSize = pagesize;
            if (!string.IsNullOrEmpty(txtHotelName.Text)) 
            {
                request.Body.keyword = txtHotelName.Text;
                request.Body.searchFields = "hotelName";
            }
            if (!string.IsNullOrEmpty(txtLocation.Text))
            {
                request.Body.keyword = txtLocation.Text;
                request.Body.searchFields = "address";
            }

            var response = HotelFacade.GetHotelList(request);
            list = response.Body.hotelList;
            if (list != null && list.Items.Count > 0)
            {
                foreach (var item in list.Items)
                {
                    getHotelRooms(item.hotelId, comeDate, leaveDate);
                }
            }
            string query = string.Format("cd={0}&ld={1}&loc={2}&n={3}", comeDate, leaveDate, txtLocation.Text, txtHotelName.Text);
            PagnationHtml = GetPageHtml("hotellist.aspx?" + query, page, list.totalPage);
        }

        void getHotelRooms(int hotelId, string comeDate, string leaveDate)
        {
            HotelRoomList hrl = new HotelRoomList();
            GetHotelRoomsRequest request = new GetHotelRoomsRequest();
            request.Body.hotelId = hotelId;
            request.Body.comeDate = comeDate;
            request.Body.leaveDate = leaveDate;
            request.Body.page = 1;
            request.Body.pageSize = 10;


            var response = HotelFacade.GetHotelRooms(request);
            var HotelRoomList = response.Body.hotelRoomList;

            if (!hotelRoomsDic.ContainsKey(hotelId))
            {
                hotelRoomsDic.Add(hotelId, HotelRoomList);
            }
        }
    }
}
